package f6sink;

import flinkemp.Emp;
import flinkemp.EmpFun;
import org.apache.flink.api.common.serialization.SimpleStringEncoder;
import org.apache.flink.core.fs.Path;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.sink.filesystem.OutputFileConfig;
import org.apache.flink.streaming.api.functions.sink.filesystem.StreamingFileSink;
import org.apache.flink.streaming.api.functions.sink.filesystem.rollingpolicies.DefaultRollingPolicy;

import java.util.concurrent.TimeUnit;

public class FileSink {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(1);
        DataStreamSource<Emp> data = env.addSource(new EmpFun());
        data.print();

        StreamingFileSink<String> flink = StreamingFileSink.<String>forRowFormat(
                new Path("./fileSink/"),
                new SimpleStringEncoder<>("UTF-8")
        )
                .withRollingPolicy(
                        DefaultRollingPolicy.builder()
                                .withRolloverInterval(TimeUnit.MINUTES.toMillis(1))
                                .withInactivityInterval(1)
                                .withMaxPartSize(1024 * 1)
                                .build()
                )
                .withOutputFileConfig(
                        OutputFileConfig.builder()
                                .withPartPrefix("flink")
                                .withPartSuffix(".txt")
                                .build()
                )
                .build();
        SingleOutputStreamOperator<String> map = data.map(emp -> emp.toString());
        map.addSink(flink);
        env.execute("FileSink");
    }
}
